﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using Pampa.CRUD.Core;
using Pampa.Grid.Data;
using Pampa.MVC.Controllers;
using Pampa.MVC.Data;
using log4net;
using log4net.Repository.Hierarchy;

namespace Pampa.MVC.Helpers
{
    public static class PampaMvc
    {
        public static Func<Type, GridDataReader> GetGridDataReader
        {
            get; set;
        }

        public static Func<string, GridDataReader> GetGridDataReader2
        {
            get; set;
        }

        /// <summary>
        /// To use this method entities have to implemtent IActiveRecord
        /// The framework will expect the entities to resolve database connection
        /// </summary>
        public static void InitializeUsingCastleActiveRecords()
        {
            EntityManager.Helper = new EntityRepositoryAR();
            PampaMvc.GetGridDataReader = (entityType => new GridDataReaderEntityAR(entityType.AssemblyQualifiedName));
            PampaMvc.GetGridDataReader2 = (entityName => new GridDataReaderEntityAR(entityName));
            ControllerBuilder.Current.DefaultNamespaces.Add("Pampa.MVC.Controllers");
        }

        /// <summary>
        /// Using this method entities may implement ICustomPersistence
        /// ICustomPersistence is only one way to override the behaviour when pampa try to persist the entities
        /// Another way would be using your own EntityRepository
        /// </summary>
        public static void InitializeUsingCastleRepositories()
        {
            EntityManager.Helper = new EntityRepositoryCastle();
            PampaMvc.GetGridDataReader = (entityType => new GridDataReaderCastle(entityType.AssemblyQualifiedName));
            PampaMvc.GetGridDataReader2 = (entityName => new GridDataReaderCastle(entityName));
            ControllerBuilder.Current.DefaultNamespaces.Add("Pampa.MVC.Controllers");
        }

        /// <summary>
        /// Initialize pampa with custom access to the database
        /// </summary>
        public static void Initialize(IEntityRepository entityRepository, Type gridDataReaderType)
        {
            if (!typeof(GridDataReader).IsAssignableFrom(gridDataReaderType))
            {
                throw new Exception(string.Format("{0} is not a valid GridDataReader", gridDataReaderType.Name));
            }

            EntityManager.Helper = entityRepository;
            PampaMvc.GetGridDataReader = (entityType => (GridDataReader)Activator.CreateInstance(gridDataReaderType, entityType.AssemblyQualifiedName));
            PampaMvc.GetGridDataReader2 = (entityName => (GridDataReader)Activator.CreateInstance(gridDataReaderType, entityName));
            ControllerBuilder.Current.DefaultNamespaces.Add("Pampa.MVC.Controllers");
        }

        /// <summary>
        /// Permite sobreescribir el mensaje para "session expired"
        /// </summary>
        public static void SessionExpiredMessage(string message)
        {
            AbstractController.SessionExpiredMessage = message;
        }

        /// <summary>
        /// Permite sobreescribir el mensaje para cualqwuier error inesperado
        /// </summary>
        public static void UnknowErrorMessage(string message)
        {
            AbstractController.UnknowErrorMessage = message;
        }
    }
}
